<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <div>
      <div></div>
      <div></div>
      <div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
      <div></div>
      <div></div>
      <div></div>
    </div>
</body>
<script>

  body = document.querySelector('body')
  //递归
  function traverseTree(node){
    if (node.children && node.children.length > 0) {
          for (var i = 0; i < node.children.length; i++) {
              node.children[i].setAttribute('title','123')
              this.traverseTree(node.children[i]);
          }
      }
  }
  //非递归 很简单的- -
  function traverseTree1(node){
    var stack = []
    stack.push(node)
    while(stack.length>0){
      var tempNode = stack.pop()
      if(tempNode.children &&tempNode.children.length> 0){
        for(var i =tempNode.children.length-1;i>0;i--){
          tempNode.children[i].setAttribute('title','456')
          stack.push(tempNode.children[i])
        }
      }
    }
  }
  traverseTree1(body);

  console.log(body)
</script>
</html>  